#!/bin/bash

# 设置颜色输出
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo -e "${GREEN}=== 修复BLESDK崩溃问题 ===${NC}"

# 导航到Example目录
echo -e "${GREEN}导航到Example目录...${NC}"
cd ~/Desktop/BLESDK/Example

# 检查Info.plist中是否包含蓝牙权限描述
echo -e "${YELLOW}检查Info.plist中的蓝牙权限...${NC}"

# 如果Info.plist存在
if [ -f "BLESDK/Info.plist" ]; then
    # 检查NSBluetoothAlwaysUsageDescription
    if ! grep -q "NSBluetoothAlwaysUsageDescription" BLESDK/Info.plist; then
        echo -e "${RED}缺少NSBluetoothAlwaysUsageDescription权限描述!${NC}"
        echo -e "${YELLOW}正在添加...${NC}"
        
        # 备份原始文件
        cp BLESDK/Info.plist BLESDK/Info.plist.backup
        
        # 在</dict>前添加权限描述
        sed -i '' 's|</dict>|    <key>NSBluetoothAlwaysUsageDescription</key>\
    <string>需要使用蓝牙来扫描和连接周围的设备</string>\
    <key>NSBluetoothPeripheralUsageDescription</key>\
    <string>需要使用蓝牙来扫描和连接周围的设备</string>\
</dict>|' BLESDK/Info.plist
        
        echo -e "${GREEN}已添加蓝牙权限描述${NC}"
    else
        echo -e "${GREEN}Info.plist已包含蓝牙权限描述√${NC}"
    fi
else
    echo -e "${RED}未找到Info.plist文件!${NC}"
fi

# 修复可能的蓝牙初始化时机问题
echo -e "${YELLOW}检查蓝牙初始化逻辑...${NC}"

# 修改AppDelegate确保蓝牙正确初始化
if [ -f "BLESDK/PREFIXAppDelegate.m" ]; then
    # 检查是否已包含正确的初始化代码
    if grep -q "\[BLESDK initSDK\]" BLESDK/PREFIXAppDelegate.m; then
        echo -e "${GREEN}AppDelegate中已包含BLESDK初始化代码√${NC}"
        
        # 在didFinishLaunchingWithOptions中添加CBCentralManager的初始化
        if ! grep -q "CBCentralManager" BLESDK/PREFIXAppDelegate.m; then
            echo -e "${YELLOW}添加CBCentralManager显式初始化代码...${NC}"
            sed -i '' '/\[BLESDK initSDK\];/a\'$'\n''    // 确保蓝牙管理器在主线程初始化\
    dispatch_async(dispatch_get_main_queue(), ^{\
        [BLEManager sharedInstance];\
    });' BLESDK/PREFIXAppDelegate.m
            
            echo -e "${GREEN}已添加CBCentralManager显式初始化代码${NC}"
        fi
    else
        echo -e "${RED}AppDelegate中未包含BLESDK初始化代码!${NC}"
        echo -e "${YELLOW}请运行verify_blesdk.sh脚本修复${NC}"
    fi
else
    echo -e "${RED}未找到PREFIXAppDelegate.m文件!${NC}"
fi

# 修复可能的framework链接问题
echo -e "${YELLOW}检查CoreBluetooth框架链接...${NC}"
if [ -f "Podfile" ]; then
    if ! grep -q "s.frameworks = 'CoreBluetooth'" ../BLESDK.podspec; then
        echo -e "${YELLOW}在podspec中添加CoreBluetooth框架依赖...${NC}"
        # 备份原始文件
        cp ../BLESDK.podspec ../BLESDK.podspec.backup
        
        # 添加框架依赖
        sed -i '' 's|# s.frameworks = "UIKit", "MapKit"|s.frameworks = "CoreBluetooth"|' ../BLESDK.podspec
        sed -i '' 's|s.frameworks = "UIKit"|s.frameworks = "CoreBluetooth"|' ../BLESDK.podspec
        sed -i '' 's|s.frameworks = |s.frameworks = "CoreBluetooth", |' ../BLESDK.podspec
        
        echo -e "${GREEN}已更新podspec${NC}"
        
        # 重新安装pod
        echo -e "${YELLOW}重新安装pod...${NC}"
        pod install
    else
        echo -e "${GREEN}podspec已包含CoreBluetooth框架依赖√${NC}"
    fi
else
    echo -e "${RED}未找到Podfile!${NC}"
fi

# 清理并重建
echo -e "${YELLOW}清理Xcode缓存...${NC}"
rm -rf ~/Library/Developer/Xcode/DerivedData/BLESDK*

echo -e "${GREEN}修复完成!${NC}"
echo -e "${YELLOW}请在Xcode中执行以下操作:${NC}"
echo -e "${YELLOW}1. 打开项目 (运行: open BLESDK.xcworkspace)${NC}"
echo -e "${YELLOW}2. 选择菜单 Product > Clean Build Folder (Shift+Command+K)${NC}"
echo -e "${YELLOW}3. 重新构建并运行项目${NC}"
echo -e "${YELLOW}4. 运行时确保设备的蓝牙已开启${NC}"
echo -e "${YELLOW}5. 第一次运行会弹出权限请求对话框，请点击"允许"${NC}" 